@extends('admin.common.base-master')

@section('html-head')
    <style type="text/css">
        .divcss5_list_title{background:#eeeeee; border:1px solid #cccccc; padding:1em;}
        .divcss5_list_content{padding:1em;}
        #tinybox{position:absolute; display:none; padding:10px; background:url('{{url('images/preload.gif')}}') no-repeat 50% 50%;  z-index:2000;}
        #tinymask{position:absolute; display:none; top:0; left:0; height:100%; width:100%; background:#000000; z-index:1500;}
        #tinycontent{background:#ffffff; font-size:1.1em;}

        #n{margin:10px auto; width:920px; border:1px solid #CCC;font-size:12px; line-height:30px;}
        #n a{ padding:0 4px; color:#333}
        .sx{padding: 10px}
        .sx-btn{width: 64px;height: 30px;background-color: #4dbcd3;color: #fff;border: #4dbcd3;border-radius: 7%;}
    </style>
    <link href="{{url('css/admin/pagenum.css')}}" rel="stylesheet" />
@endsection

@section('html-body')
    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <a href="#">产业设置</a>
        </div>

        <div class="filter-box clearfix">
            <div class="fr" style="margin-top: 10px;">
                <a class="addBtn" href="javascript:;" onclick="upPop(0,0,'')">新增</a>
            </div>
        </div>

        <div class="table-box">
            <table class="table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>产业名称</th>
                    <th>子分类</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @if(count($functions) > 0)
                    @foreach($functions as $function)
                        <tr>
                            <td>{{$function->id}}</td>
                            <td>{{$function->name}}</td>
                            <td>
                                @if(count($function->childs) > 0)
                                    @foreach($function->childs as $menuChild)
                                        <p>
                                            {{$menuChild->name}}
                                            <a href="javascript:;" class="color2 del" style="margin-left: 5px" data-id="{{$menuChild->id}}">删除</a>
                                        </p>
                                    @endforeach
                                @endif
                            </td>
                            <td>
                                <a class="color2" href="javascript:;" onclick="upPop(0,'{{$function->id}}','')">新增子产业</a>
                                <span> | </span>
                                <a class="color5" href="javascript:;" onclick="upPop('{{$function->id}}',0,'{{$function->name}}')">编辑</a>
                                <span> | </span>
                                <a class="color5 del" href="javascript:;" data-id="{{$function->id}}">删除</a>
                            </td>
                        </tr>
                    @endforeach
                @endif
                </tbody>
                <tfoot class="order-gl">

                </tfoot>
            </table>
        </div>
    </div>

    <div class="upPop-box">
        <div class="backdrop"></div>
        <div id="upPop" class="info-pop tixian">
            <a class="close" href="javascript:void(0);"></a>
            <h3 id="title">新增职业</h3>

            <div class="fill-list">
                <span class="fill-tit">栏目名称：</span>
                <input class="fill-ipt" type="text" name="name" id="name">
            </div>

            <input type="hidden" id="pid" value="" />
            <input type="hidden" id="pro_id" value="" />

            <div class="fill-list">
                <span class="fill-tit">&nbsp;</span>
                <input class="fill-submit" type="button" id="subThisForm" value="提交">
            </div>
        </div>
    </div>
    <script>
        function upPop(id,pid,name) {
            $("#pid").val(pid);
            $("#pro_id").val(id);
            if(pid == 0 && id == 0){
                $("#title").html("新增产业");
            }else if(id != 0 && pid == 0){
                $("#title").html("修改产业名称");
            }else if( id == 0 && pid != 0){
                $("#title").html("新增子产业");
            }

            $("#name").val(name);

            $('.upPop-box').upPop();
        }
        $(function () {

            $(document).on('click', '#subThisForm', function () {
                var pid    = $("#pid").val();
                var pro_id = $("#pro_id").val();
                var name   = $("#name").val();

                $.ajax({
                    url: '{{route('Api.Admin.Sys.setIndustry')}}',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        pid: pid,
                        name:name,
                        id:pro_id
                    },
                    success: function (obj) {
                        if (obj.level == "success") {
                            noty({
                                text: obj.message, type: obj.level, timeout: 600,
                                callback: {
                                    afterClose: function () {
                                        window.location.reload();
                                    }
                                }
                            });
                        } else {
                            noty({type: obj.level, text: obj.message, timeout: 600});
                        }
                    },
                    error: function (obj) {
                        console.log(obj);
                    }

                });
            });

            $(document).on("click", ".del", function () {
                var id = $(this).attr("data-id");

                noty({
                    text: '将影响用户资料填写，你确定要删除吗?',
                    buttons: [
                        {
                            addClass: 'btn btn-primary', text: '确定', onClick: function ($noty) {
                                $noty.close();
                                $.ajax({
                                    url: '{{route('Api.Admin.Sys.delIndustry')}}',
                                    type: 'post',
                                    dataType: 'json',
                                    data: {id: id},
                                    success: function (obj) {
                                        if (obj.level == "success") {
                                            window.location.reload();
                                        } else {
                                            noty({type: obj.level, text: obj.message, timeout: 600});
                                        }
                                    },
                                    error: function (obj) {
                                        console.log(obj);
                                    }

                                });
                            }
                        },
                        {
                            addClass: 'btn btn-danger', text: '取消', onClick: function ($noty) {
                                $noty.close();
                            }
                        }
                    ]
                });
            });
        })
    </script>
@endsection